package edu.njfu.wigetdemo.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import edu.njfu.wigetdemo.R
import edu.njfu.wigetdemo.model.Fruit
import kotlinx.android.synthetic.main.fruit_item_layout.view.*

class FruitAdapter2(val fruitList:List<Fruit>):RecyclerView.Adapter<FruitAdapter2.ViewHolder>() {

    inner class ViewHolder(view: View):RecyclerView.ViewHolder(view){
        val fruitImage: ImageView = view.findViewById(R.id.fruitImage)
        val fruitName: TextView =view.findViewById(R.id.fruitName)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        //TODO("Not yet implemented")
        val view=LayoutInflater.from(parent.context)
            .inflate(R.layout.fruit_item_layout2,parent,false)
        val viewHolder=ViewHolder(view)
        viewHolder.itemView.setOnClickListener {
            val position=viewHolder.adapterPosition;
            val fruit=fruitList[position]
            Toast.makeText(parent.context,"你点击了${fruit.name}", Toast.LENGTH_LONG).show()
        }
        viewHolder.fruitImage.setOnClickListener {
            val position=viewHolder.adapterPosition;
            val fruit=fruitList[position]
            Toast.makeText(parent.context,"你点击了${fruit.name}", Toast.LENGTH_LONG).show()
        }
        return  viewHolder
    }

    override fun getItemCount(): Int {
        //TODO("Not yet implemented")
        return  fruitList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        //TODO("Not yet implemented")
        val fruit=fruitList[position]
        holder.fruitImage.setImageResource(fruit.imageId)
        holder.fruitName.text=fruit.name
    }

}